package io.tim.timongodb;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.tim.timongodb.util.ClassUtil;
import io.tim.timongodb.util.InstanceXMLBuilder;

/**
 * 数据库实例对象
 *
 * @author zhangnan
 * @date 2017年4月24日
 *
 */
public class DBInstance {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(ClassUtil.class);
	
	private String instanceName = "";
	private String instanceFileName = ".xml";
	private String instancePath = "src/main/resources/instances/";
	private Map<String, Database> databases = null;
	
	public DBInstance(String instanceName) {
		this.instanceName = instanceName;
		this.instanceFileName = this.instanceName + this.instanceFileName;
		InstanceXMLBuilder.NewInstanceXML(this.instanceName, this.instancePath, this.instanceFileName);
		databases = new HashMap<String, Database>();
	}
	
	public Map<String, Database> addDatabase(String dbName, Database db) {
		if (databases.containsKey(dbName)) {
			LOGGER.error("the database " + dbName + " has already existed in the instance!");
		} else {
			databases.put(dbName, db);
		}
		return databases;
	}
	
	public Map<String, Database> dropDatabase(String dbName) {
		databases.remove(dbName);
		return databases;
	}

	public String getInstanceName() {
		return instanceName;
	}

	public void setInstanceName(String instanceName) {
		this.instanceName = instanceName;
	}
	
	public String getInstancePath() {
		return instancePath;
	}

	public void setInstancePath(String instancePath) {
		this.instancePath = instancePath;
	}

	public String getInstanceFileName() {
		return instanceFileName;
	}

	public void setInstanceFileName(String instanceFileName) {
		this.instanceFileName = instanceFileName;
	}

	public Map<String, Database> getDatabases() {
		return databases;
	}

	public void setDatabases(Map<String, Database> databases) {
		this.databases = databases;
	}

	@Override
	public String toString() {
		return "DBInstance [instanceName=" + instanceName + ", databases=" + databases + "]";
	}
	
	public static void main(String[] args) {
		DBInstance dbInst2 = new DBInstance("dbInst2");
	}
	
}
